#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;

namespace AirWeb.Security
{
	/// <summary>
	/// This class represents an access policy Ilevel) for a given resource.
	/// Currently only None,Read,Write are supported
	/// </summary>
	/// 

	[Serializable]
	public class Right
	{
		/// <summary>
		/// Enumeration of possible access rights
		/// </summary>
		public enum AccessLevel
		{
			/// <summary>
			/// User is not allowed to access the resource. Used in deny right scenarios
			/// </summary>
			None = 0,
			/// <summary>
			/// User is allowed to red the resource
			/// </summary>
			Read = 1,
			/// <summary>
			/// User is allowed to modify the resource
			/// </summary>
			Write = 2
		}

		AccessLevel m_AccessLevel = AccessLevel.None;
		IResource m_Resource;

		/// <summary>
		/// The resource protected by this right
		/// </summary>
		public IResource Resource
		{
			get
			{
				return m_Resource;
			}
		}

		/// <summary>
		/// The Access Level to the resource (None,Read,Write etc.)
		/// </summary>
		public AccessLevel Access
		{
			get
			{
				return m_AccessLevel;
			}
		}

		/// <summary>
		/// Constructor. 
		/// </summary>
		/// <param name="i_Resource">A respirce that this righr protrcts</param>
		/// <param name="i_right">Allowed access to the resource</param>
		public Right(IResource i_Resource, AccessLevel i_right)
		{
			m_Resource = i_Resource;
			m_AccessLevel = i_right;
		}

		/// <summary>
		/// Checks if the resource that this right represents matches the supplied resource
		/// </summary>
		/// <param name="i_other">The resourcd to be checked</param>
		/// <returns></returns>
		public bool Matches(IResource i_other)
		{
			return m_Resource.Matches(i_other);
		}

		/// <summary>
		/// Check if the AccessLevel of this object is less rtestricted than requested
		/// </summary>
		/// <param name="i_access">AccessLevel to check</param>
		/// <returns>True if this.AccessLevel is LESS or EQUALLY restricted than requested. For example if this.AccessLevel is WRITE
		/// and i_access is READ it will resturn true.</returns>
		public bool HasMinimumAccess(AccessLevel i_access)
		{
			int lhs = (int)i_access;
			int rhs = (int)this.m_AccessLevel;

			return lhs <= rhs;
		}
	}
}
